package com.moguhoutai.controller;

import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.google.gson.Gson;
import com.moguhoutai.biz.AreaBiz;
import com.moguhoutai.biz.CityBiz;
import com.moguhoutai.biz.HouseBiz;
import com.moguhoutai.biz.LandlordBiz;
import com.moguhoutai.biz.ModeBiz;
import com.moguhoutai.biz.ResourceBiz;
import com.moguhoutai.biz.SubwayBiz;
import com.moguhoutai.biz.payBiz;
import com.moguhoutai.dao.ModeMapper;
import com.moguhoutai.model.Admin;
import com.moguhoutai.model.Area;
import com.moguhoutai.model.City;
import com.moguhoutai.model.House;
import com.moguhoutai.model.Resources;
import com.moguhoutai.model.Subway;
import com.moguhoutai.util.BusinessException;
import com.moguhoutai.util.Constants;
@Controller
@RequestMapping("/house")
public class HouseControler {
    @Resource
	private HouseBiz houseBiz;
    @Resource
    private CityBiz cBiz;
    
    @Resource
    private AreaBiz  areaBiz;
    
    @Resource
    private SubwayBiz  subiz;
    
    @Resource
    private ModeBiz  mBiz;
    
    @Resource
    private payBiz  pBiz;
    
    @Resource
    private LandlordBiz  lBiz;
    
    @Resource
    private ResourceBiz  reBiz;
    
    @RequestMapping("/list")
	public String list(Map<String, Object> request){
		try {
			long count = houseBiz.findCount();
			List<House> list = houseBiz.findAll();
			request.put("totalCount", count);
			request.put("list", list);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "/house/list";
	}
    
    
    @RequestMapping("/findBycity")
	public void findBycity(int cityId, PrintWriter out){
		try {
			List<Area> list = areaBiz.findBycity(cityId);
			Gson gson=new Gson();
			String strgson=gson.toJson(list);
			out.print(strgson);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}
    
    
    @RequestMapping("/findSuBycity")
   	public void findSuBycity(int cityId, PrintWriter out){
   		try {
   			List<Subway> list = subiz.findSuBycity(cityId);
   			Gson gson=new Gson();
   			String strgson=gson.toJson(list);
   			out.print(strgson);
   		} catch (BusinessException e) {
   			e.printStackTrace();
   		}
   	}
    
    @RequestMapping("/preAdd")
	public String preAdd(Map<String, Object> request){
		try {
			request.put("house", new House());
			List<City> list = cBiz.findAll();
			request.put("list", list);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "/house/add";
	}
    
    
    
    @ResponseBody
	@RequestMapping("/add")
	public String add(House house, MultipartHttpServletRequest request, HttpSession session){
		try {
			Admin admin = (Admin) session.getAttribute("currentUser");
			MultipartFile file = request.getFile("file");
			String fileName = UUID.randomUUID().toString();
			File newFile = new File(Constants.FILE_PATH + fileName);
			file.transferTo(newFile);
			Resources res = new Resources();
			res.setResId(fileName);
			res.setResName(file.getOriginalFilename());
			res.setResPath(fileName);
			res.setCreator(admin.getTrueName());
			res.setUpdator(admin.getTrueName());
			house.setRes(res);
			house.setCreator(admin.getTrueName());
			house.setUpdator(admin.getTrueName());
			houseBiz.save(house);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:list";
	}
    
    
    @ResponseBody
    @RequestMapping("/delete/{houseId}")
    public String  delete(@PathVariable int houseId){
    	try {
			House house =houseBiz.findById(houseId);
    		if(house==null){
    			return "redirect:list";
    		}
    		houseBiz.delete(house);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
    	return "redirect:list";
    }
    
    @RequestMapping("/preUpdate/{houseId}")
	public String preUpdate(@PathVariable int houseId, Map<String, Object> request){
		try {
			House house = houseBiz.findById(houseId);
			request.put("house", house);
			List<City> list = cBiz.findAll();
			request.put("CityList", list);
			List<Area> list1 = areaBiz.findBycity(house.getArea().getCity().getCityId());
			request.put("AreaList", list1);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "/house/update";
	}
    
    
}
